package org.example.service.impl;

import org.example.dao.BuildingDao;
import org.example.dto.BuildingDto;
import org.example.dto.ClassDto;
import org.example.dto.ClassroomDto;
import org.example.pojo.Building;
import org.example.pojo.Classes;
import org.example.pojo.Classroom;
import org.example.service.BuildingService;
import org.example.vo.BuildingVo;
import org.example.vo.ClassroomVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Resource
    private BuildingDao dao;

    private BuildingDto PoTransToDto(Building building){
        BuildingDto buildingDto=new BuildingDto();
        buildingDto.setBuildingId(building.getBuildingno());
        buildingDto.setBuildingName(building.getBuildingname());
        return buildingDto;
    }

    private Building VoTransToPo(BuildingVo buildingVo){
        Building building=new Building();
        building.setBuildingname(buildingVo.getBuildingName());
        building.setBuildingno(buildingVo.getBuildingId());
        return building;
    }

    @Override
    public Integer addBuilding(BuildingVo building) {
        if(dao.insert(VoTransToPo(building)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateBuilding(BuildingVo building) {
        if(dao.update(VoTransToPo(building)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteBuilding(Integer buildingId) {
        if(dao.delete(buildingId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<BuildingDto> queryAllBuilding() {
        List<Building> buildingList=dao.queryAll();
        List<BuildingDto> buildingDtoList=new ArrayList<>();
        for(int i=0;i<buildingList.size();i++)
            buildingDtoList.add(i,PoTransToDto(buildingList.get(i)));
        return buildingDtoList;
    }

    @Override
    public Integer countBuilding() {
        return dao.queryAll().size();
    }

    @Override
    public BuildingDto queryBuildingByName(String buildingName) {
        Building building=new Building();
        building.setBuildingname(buildingName);
        return PoTransToDto(dao.queryByCondition(building).get(0));
    }

    @Override
    public BuildingDto queryBuildingById(Integer buildingId) {
        Building building=new Building();
        building.setBuildingno(buildingId);
        return PoTransToDto(dao.queryByCondition(building).get(0));
    }

}
